package lyx.game
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import lyx.game.control.IKeyControlTarget;
	import lyx.game.utils.GameState;
	import lyx.game.utils.KeySetting;
	import lyx.game.utils.KeyState;
	import lyx.utils.log.Logger;
	
	public class GameEngine implements IKeyControlTarget
	{	
		private var _game_state:int = -1;
		public function get game_state():int
		{
			return _game_state;
		}
		
		protected var _engine_state_handler:Function;
		protected var _engine_thread_interval:int = 50;
		protected var _engine_thread:Timer;
		
		protected var _time:int;
		
		/**
		 * 实现IKeyControlTarget的actionByKeysState方法，
		 * 在GameEngine中主要监听暂停按键，和退出按键的处理。 
		 * @param state
		 * @param keyset
		 * 
		 */		
		public function actionByKeysState(state:KeyState,keyset:KeySetting):void
		{
			if(state.last_is_keydown)
			{
				var key:int = state.last_keycode;
				switch(key)
				{
					case keyset.start:
						if(game_state==GameState.GAME_START)
						{
							switchGameState(GameState.GAME_RUNNING);
						}
						else if(game_state==GameState.GAME_OVER)
						{
							switchGameState(GameState.GAME_START);
						}
						break;
					case keyset.select:
						if(game_state==GameState.GAME_RUNNING)
						{
							switchGameState(GameState.GAME_PAUSE);
						}
						else if(game_state==GameState.GAME_PAUSE)
						{
							switchGameState(GameState.GAME_RUNNING);
						}
						break;
					case keyset.function_0:
						switchGameState(GameState.GAME_OVER);
						break;
				}
			}
		}
		
		/**
		 * 启动游戏引擎
		 */
		final public function start(init_state:int=GameState.GAME_START):void
		{
			Logger.getInstance().debug("Start Game Engine");
			
			switchGameState(init_state);
			
			_engine_thread = new Timer(_engine_thread_interval);
			_engine_thread.addEventListener(TimerEvent.TIMER,engineThreadHandler);
			_engine_thread.start();
		}
		
		private function engineThreadHandler(event:TimerEvent):void
		{
			if(_engine_state_handler!=null)
			{
				_engine_state_handler.apply(this);
			}
		}
		
		/**
		 * 改变游戏引擎所处状态，次函数会出发游戏引擎执行的处理函数的改变。
		 */
		final public function switchGameState(state:int):void
		{
			_time = getTimer();
			
			var old_state:int = _game_state;
			_game_state = state;
			
			switch(_game_state)
			{
				case GameState.GAME_START:
					Logger.getInstance().debug("gameStartHandler()");
					_engine_state_handler = gameStartHandler;
					break;
				case GameState.GAME_RUNNING:
					Logger.getInstance().debug("gameRunningHandler()");
					_engine_state_handler = gameRunningHandler;
					break;
				case GameState.GAME_PAUSE:
					Logger.getInstance().debug("gamePauseHandler()");
					_engine_state_handler = gamePauseHandler;
					break;
				case GameState.GAME_OVER:
					Logger.getInstance().debug("gameOverHandler()");
					_engine_state_handler = gameOverHandler;
					break;
				default:
					Logger.getInstance().debug("Unknow game state");
					_engine_state_handler = null;
					_game_state = old_state; 
					break;
			}
		}
		
		/**
		 * game_state=GameState.GAME_START的处理函数
		 */
		protected function gameStartHandler():void
		{
		}
		
		/**
		 * game_state=GameState.GAME_STARTED的处理函数
		 */
		protected function gameRunningHandler():void
		{
		}
		
		/**
		 * game_state=GameState.GAME_PAUSE的处理函数
		 */
		protected function gamePauseHandler():void
		{
		}
		
		/**
		 * game_state=GameState.GAME_OVER的处理函数
		 */
		protected function gameOverHandler():void
		{
		}

	}
}